
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>django.template.backends.django &#8212; Django 2.2.12.dev20200304094918 documentation</title>
    <link rel="stylesheet" href="../../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" />



 
<script type="text/javascript" src="../../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../../index.html">Django 2.2.12.dev20200304094918 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-template-backends-django">
            
  <h1>Source code for django.template.backends.django</h1><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">importlib</span> <span class="k">import</span> <span class="n">import_module</span>
<span class="kn">from</span> <span class="nn">pkgutil</span> <span class="k">import</span> <span class="n">walk_packages</span>

<span class="kn">from</span> <span class="nn">django.apps</span> <span class="k">import</span> <span class="n">apps</span>
<span class="kn">from</span> <span class="nn">django.conf</span> <span class="k">import</span> <span class="n">settings</span>
<span class="kn">from</span> <span class="nn">django.template</span> <span class="k">import</span> <span class="n">TemplateDoesNotExist</span>
<span class="kn">from</span> <span class="nn">django.template.context</span> <span class="k">import</span> <span class="n">make_context</span>
<span class="kn">from</span> <span class="nn">django.template.engine</span> <span class="k">import</span> <span class="n">Engine</span>
<span class="kn">from</span> <span class="nn">django.template.library</span> <span class="k">import</span> <span class="n">InvalidTemplateLibrary</span>

<span class="kn">from</span> <span class="nn">.base</span> <span class="k">import</span> <span class="n">BaseEngine</span>


<div class="viewcode-block" id="DjangoTemplates"><a class="viewcode-back" href="../../../../topics/templates.html#django.template.backends.django.DjangoTemplates">[docs]</a><span class="k">class</span> <span class="nc">DjangoTemplates</span><span class="p">(</span><span class="n">BaseEngine</span><span class="p">):</span>

    <span class="n">app_dirname</span> <span class="o">=</span> <span class="s1">&#39;templates&#39;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">params</span><span class="p">):</span>
        <span class="n">params</span> <span class="o">=</span> <span class="n">params</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="n">options</span> <span class="o">=</span> <span class="n">params</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">&#39;OPTIONS&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="n">options</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="s1">&#39;autoescape&#39;</span><span class="p">,</span> <span class="kc">True</span><span class="p">)</span>
        <span class="n">options</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="s1">&#39;debug&#39;</span><span class="p">,</span> <span class="n">settings</span><span class="o">.</span><span class="n">DEBUG</span><span class="p">)</span>
        <span class="n">options</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="s1">&#39;file_charset&#39;</span><span class="p">,</span> <span class="n">settings</span><span class="o">.</span><span class="n">FILE_CHARSET</span><span class="p">)</span>
        <span class="n">libraries</span> <span class="o">=</span> <span class="n">options</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;libraries&#39;</span><span class="p">,</span> <span class="p">{})</span>
        <span class="n">options</span><span class="p">[</span><span class="s1">&#39;libraries&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_templatetag_libraries</span><span class="p">(</span><span class="n">libraries</span><span class="p">)</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">params</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">engine</span> <span class="o">=</span> <span class="n">Engine</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">dirs</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">app_dirs</span><span class="p">,</span> <span class="o">**</span><span class="n">options</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">from_string</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">template_code</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">Template</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">engine</span><span class="o">.</span><span class="n">from_string</span><span class="p">(</span><span class="n">template_code</span><span class="p">),</span> <span class="bp">self</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_template</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">template_name</span><span class="p">):</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">Template</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">engine</span><span class="o">.</span><span class="n">get_template</span><span class="p">(</span><span class="n">template_name</span><span class="p">),</span> <span class="bp">self</span><span class="p">)</span>
        <span class="k">except</span> <span class="n">TemplateDoesNotExist</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
            <span class="n">reraise</span><span class="p">(</span><span class="n">exc</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_templatetag_libraries</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">custom_libraries</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Return a collation of template tag libraries from installed</span>
<span class="sd">        applications and the supplied custom_libraries argument.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">libraries</span> <span class="o">=</span> <span class="n">get_installed_libraries</span><span class="p">()</span>
        <span class="n">libraries</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">custom_libraries</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">libraries</span></div>


<span class="k">class</span> <span class="nc">Template</span><span class="p">:</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">template</span><span class="p">,</span> <span class="n">backend</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">template</span> <span class="o">=</span> <span class="n">template</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">backend</span> <span class="o">=</span> <span class="n">backend</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">origin</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">template</span><span class="o">.</span><span class="n">origin</span>

    <span class="k">def</span> <span class="nf">render</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">context</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">request</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="n">context</span> <span class="o">=</span> <span class="n">make_context</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">autoescape</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">backend</span><span class="o">.</span><span class="n">engine</span><span class="o">.</span><span class="n">autoescape</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">template</span><span class="o">.</span><span class="n">render</span><span class="p">(</span><span class="n">context</span><span class="p">)</span>
        <span class="k">except</span> <span class="n">TemplateDoesNotExist</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
            <span class="n">reraise</span><span class="p">(</span><span class="n">exc</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">backend</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">copy_exception</span><span class="p">(</span><span class="n">exc</span><span class="p">,</span> <span class="n">backend</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Create a new TemplateDoesNotExist. Preserve its declared attributes and</span>
<span class="sd">    template debug data but discard __traceback__, __context__, and __cause__</span>
<span class="sd">    to make this object suitable for keeping around (in a cache, for example).</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">backend</span> <span class="o">=</span> <span class="n">backend</span> <span class="ow">or</span> <span class="n">exc</span><span class="o">.</span><span class="n">backend</span>
    <span class="n">new</span> <span class="o">=</span> <span class="n">exc</span><span class="o">.</span><span class="vm">__class__</span><span class="p">(</span><span class="o">*</span><span class="n">exc</span><span class="o">.</span><span class="n">args</span><span class="p">,</span> <span class="n">tried</span><span class="o">=</span><span class="n">exc</span><span class="o">.</span><span class="n">tried</span><span class="p">,</span> <span class="n">backend</span><span class="o">=</span><span class="n">backend</span><span class="p">,</span> <span class="n">chain</span><span class="o">=</span><span class="n">exc</span><span class="o">.</span><span class="n">chain</span><span class="p">)</span>
    <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">exc</span><span class="p">,</span> <span class="s1">&#39;template_debug&#39;</span><span class="p">):</span>
        <span class="n">new</span><span class="o">.</span><span class="n">template_debug</span> <span class="o">=</span> <span class="n">exc</span><span class="o">.</span><span class="n">template_debug</span>
    <span class="k">return</span> <span class="n">new</span>


<span class="k">def</span> <span class="nf">reraise</span><span class="p">(</span><span class="n">exc</span><span class="p">,</span> <span class="n">backend</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Reraise TemplateDoesNotExist while maintaining template debug information.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">new</span> <span class="o">=</span> <span class="n">copy_exception</span><span class="p">(</span><span class="n">exc</span><span class="p">,</span> <span class="n">backend</span><span class="p">)</span>
    <span class="k">raise</span> <span class="n">new</span> <span class="kn">from</span> <span class="nn">exc</span>


<span class="k">def</span> <span class="nf">get_installed_libraries</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Return the built-in template tag libraries and those from installed</span>
<span class="sd">    applications. Libraries are stored in a dictionary where keys are the</span>
<span class="sd">    individual module names, not the full module paths. Example:</span>
<span class="sd">    django.templatetags.i18n is stored as i18n.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">libraries</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="n">candidates</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;django.templatetags&#39;</span><span class="p">]</span>
    <span class="n">candidates</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span>
        <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">.templatetags&#39;</span> <span class="o">%</span> <span class="n">app_config</span><span class="o">.</span><span class="n">name</span>
        <span class="k">for</span> <span class="n">app_config</span> <span class="ow">in</span> <span class="n">apps</span><span class="o">.</span><span class="n">get_app_configs</span><span class="p">())</span>

    <span class="k">for</span> <span class="n">candidate</span> <span class="ow">in</span> <span class="n">candidates</span><span class="p">:</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">pkg</span> <span class="o">=</span> <span class="n">import_module</span><span class="p">(</span><span class="n">candidate</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
            <span class="c1"># No templatetags package defined. This is safe to ignore.</span>
            <span class="k">continue</span>

        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">pkg</span><span class="p">,</span> <span class="s1">&#39;__path__&#39;</span><span class="p">):</span>
            <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">get_package_libraries</span><span class="p">(</span><span class="n">pkg</span><span class="p">):</span>
                <span class="n">libraries</span><span class="p">[</span><span class="n">name</span><span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="n">candidate</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">:]]</span> <span class="o">=</span> <span class="n">name</span>

    <span class="k">return</span> <span class="n">libraries</span>


<span class="k">def</span> <span class="nf">get_package_libraries</span><span class="p">(</span><span class="n">pkg</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Recursively yield template tag libraries defined in submodules of a</span>
<span class="sd">    package.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">for</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">walk_packages</span><span class="p">(</span><span class="n">pkg</span><span class="o">.</span><span class="n">__path__</span><span class="p">,</span> <span class="n">pkg</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">+</span> <span class="s1">&#39;.&#39;</span><span class="p">):</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">module</span> <span class="o">=</span> <span class="n">import_module</span><span class="p">(</span><span class="n">entry</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
        <span class="k">except</span> <span class="ne">ImportError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">InvalidTemplateLibrary</span><span class="p">(</span>
                <span class="s2">&quot;Invalid template library specified. ImportError raised when &quot;</span>
                <span class="s2">&quot;trying to load &#39;</span><span class="si">%s</span><span class="s2">&#39;: </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">entry</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">e</span><span class="p">)</span>
            <span class="p">)</span>

        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">module</span><span class="p">,</span> <span class="s1">&#39;register&#39;</span><span class="p">):</span>
            <span class="k">yield</span> <span class="n">entry</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Mar 04, 2020</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>